home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / importers and exporters / carbon qt graphic import / source / navservices.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.3 KB  |  334 lines

  1. /*
  2.     File:        NavServices.c
  3.     
  4.     Description:Code that calls NavServices and handles Nav events sent back to the application.
  5.  
  6.     Author:        MC
  7.  
  8.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                 ("Apple") in consideration of your agreement to the following terms, and your
  12.                 use, installation, modification or redistribution of this Apple software
  13.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                 please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                 In consideration of your agreement to abide by the following terms, and subject
  17.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                 copyrights in this original Apple software (the "Apple Software"), to use,
  19.                 reproduce, modify and redistribute the Apple Software, with or without
  20.                 modifications, in source and/or binary forms; provided that if you redistribute
  21.                 the Apple Software in its entirety and without modifications, you must retain
  22.                 this notice and the following text and disclaimers in all such redistributions of
  23.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                 Apple Software without specific prior written permission from Apple.  Except as
  26.                 expressly stated in this notice, no other rights or licenses, express or implied,
  27.                 are granted by Apple herein, including but not limited to any patent rights that
  28.                 may be infringed by your derivative works or by other works in which the Apple
  29.                 Software may be incorporated.
  30.  
  31.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                 COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.                 
  45.     Change History (most recent first):
  46.  
  47. */
  48.  
  49. #define TARGET_API_MAC_CARBON 1
  50.  
  51. #include "NavServices.h"
  52.  
  53. Movie                                gPreviewMovie;
  54. MovieController                        gPreviewMC;
  55. long                                gLastFilesID;
  56. extern OSType *                        gAcceptedFlavorTypes;
  57. extern long                            gNumGIComponents;
  58.  
  59. static pascal Boolean NavFileFilterProc (AEDesc* theItem, void* info, NavCallBackUserData callBackUD, NavFilterModes filterMode) {
  60. #pragma unused (theItem, callBackUD, filterMode)
  61.     Boolean                        display;
  62.     NavFileOrFolderInfo*        theInfo;
  63.     int                            i;
  64.  
  65.     theInfo = (NavFileOrFolderInfo*)info;
  66.     i = 0;
  67.     display = false;
  68.  
  69.     do {
  70.         if (theInfo->isFolder) {
  71.             display = true;
  72.         } else {
  73.             if (theInfo->fileAndFolder.fileInfo.finderInfo.fdType == gAcceptedFlavorTypes[i++] || 
  74.                 theInfo->fileAndFolder.fileInfo.finderInfo.fdType == MovieFileType) {
  75.                 display = true;    // Show this item
  76.             }
  77.         }
  78.     } while (display == false && i <= gNumGIComponents);
  79.  
  80.     return display;
  81. }
  82.  
  83. static pascal Boolean NavPreviewProc (NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
  84. #pragma unused (callBackUD)
  85.     ComponentResult                err;
  86.     Boolean                        prevVis;
  87.     AEDesc                        selectionDesc;
  88.     FSSpec                        finalFSSpec;
  89.     GraphicsImportComponent        giFromQT;
  90.     Rect                        boundsRect;
  91.     long                        count,
  92.                                 fileID;
  93.     short                        resRefNum;
  94.     HParamBlockRec                pb;
  95.     DescType                    returnedType;
  96.     Size                        actualSize;
  97.     AEKeyword                    keywd;
  98.  
  99.     err = NavCustomControl (callBackParms->context, kNavCtlIsPreviewShowing, &prevVis);
  100.     if (prevVis == true) {
  101.         err = NavCustomControl (callBackParms->context, kNavCtlGetSelection, &selectionDesc);
  102.  
  103.         if (err == noErr) {
  104.             (void)AECountItems (&selectionDesc, &count);
  105.             // Just preview the last thing they selected (if they selected multiple items)
  106.             err = AEGetNthPtr (&selectionDesc, count, typeFSS, &keywd, &returnedType, &finalFSSpec, sizeof (FSSpec), &actualSize);
  107.             if (err == noErr) {
  108.                 CursHandle        cursor = nil;
  109.  
  110.                 cursor = GetCursor (watchCursor);
  111.                 SetCursor (*cursor);
  112.                 err = GetGraphicsImporterForFile (&finalFSSpec, &giFromQT);
  113.                 if (err != noErr) {
  114.                     // Don't stop the movie if we are asked to preview the
  115.                     // same movie we are currently previewing.
  116.                     pb.fidParam.ioCompletion = nil;
  117.                     pb.fidParam.ioNamePtr = finalFSSpec.name;
  118.                     pb.fidParam.ioVRefNum = finalFSSpec.vRefNum;
  119.                     pb.fidParam.ioSrcDirID = finalFSSpec.parID;
  120.                     (void)PBCreateFileIDRefSync (&pb);
  121.                     fileID = pb.fidParam.ioFileID;
  122.  
  123.                     err = noErr;
  124.                     if (fileID != gLastFilesID) {
  125.                         // Preview a new movie
  126.                         gLastFilesID = fileID;
  127.                         if (gPreviewMovie != nil) {
  128.                             StopMovie (gPreviewMovie);
  129.                             DisposeMovieController (gPreviewMC);
  130.                             DisposeMovie (gPreviewMovie);
  131.                         }
  132.  
  133.                         err = OpenMovieFile (&finalFSSpec, &resRefNum, fsRdPerm);
  134.  
  135.                         if (err == noErr) {
  136.                             err = NewMovieFromFile (&gPreviewMovie, resRefNum, 0, nil, newMovieActive, nil);
  137.                             CloseMovieFile (resRefNum);
  138.                         }
  139.  
  140.                         if (err == noErr) {
  141.                             GetMovieNaturalBoundsRect (gPreviewMovie, &boundsRect);
  142.                             // Set movie's origin to 0,0.
  143.                             boundsRect.bottom -= boundsRect.top;
  144.                             boundsRect.bottom += 16;    // Room for the controller.
  145.                             boundsRect.right -= boundsRect.left;
  146.                             boundsRect.top = 0;
  147.                             boundsRect.left = 0;
  148.                             ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
  149.                             gPreviewMC = NewMovieController (gPreviewMovie, &boundsRect, mcScaleMovieToFit);
  150.                         }
  151.  
  152.                         if (gPreviewMC != nil) {
  153.                             (void)MCDraw (gPreviewMC, callBackParms->window);
  154.                         }
  155.                     } else {
  156.                         // Just update the preview.
  157.                         (void)MCDraw (gPreviewMC, callBackParms->window);
  158.                     }
  159.                 } else {
  160.                     if (gPreviewMovie != nil) {
  161.                         StopMovie (gPreviewMovie);
  162.                         DisposeMovieController (gPreviewMC);
  163.                         DisposeMovie (gPreviewMovie);
  164.                         gPreviewMC = nil;
  165.                         gPreviewMovie = nil;
  166.                         gLastFilesID = 0;
  167.                     }
  168.                     // If it's not a movie, try it as an image file.
  169.                     // giFromQT is already valid because we got it at the start
  170.                     // of this function.
  171.                     err = GraphicsImportGetBoundsRect (giFromQT, &boundsRect);
  172.  
  173.                     if (err == noErr) {
  174.                         ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
  175.                         err = GraphicsImportSetBoundsRect (giFromQT, &boundsRect);
  176.                     }
  177.  
  178.                     if (err == noErr) {
  179.                         err = GraphicsImportDraw (giFromQT);
  180.                     }
  181.  
  182.                     if (giFromQT != nil) {
  183.                         (void)CloseComponent (giFromQT);
  184.                     }
  185.                 }
  186.                 cursor = (CursHandle)NewHandle (sizeof (Cursor));
  187.                 GetQDGlobalsArrow (*cursor);
  188.                 SetCursor (*cursor);
  189.                 DisposeHandle ((Handle)cursor);
  190.             }
  191.         }
  192.     }
  193.  
  194.     (void)AEDisposeDesc (&selectionDesc);
  195.  
  196.     if (err == noErr) {
  197.         return true;
  198.     } else {
  199.         return false;
  200.     }
  201. }
  202.  
  203. static pascal void NavEventProc (NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, NavCallBackUserData callBackUD) {
  204. #pragma unused (callBackUD)
  205.     OSErr                        err;
  206.     Point                        where;
  207.     Rect                        boundsRect;
  208.  
  209.     switch (callBackSelector) {
  210.         case kNavCBEvent:
  211.             if ((gPreviewMC != nil && MCIsPlayerEvent (gPreviewMC, callBackParms->eventData.eventDataParms.event) == 0) || gPreviewMC == nil) {
  212.                 switch (callBackParms->eventData.eventDataParms.event->what) {
  213.                     case nullEvent:
  214.                         MCIdle (gPreviewMC);
  215.                         break;
  216.                     case updateEvt:
  217.                         err = DispatchWindowUpdate ((WindowPtr)callBackParms->eventData.eventDataParms.event->message);
  218.                         break;
  219.                     case mouseDown:
  220.                         where = callBackParms->eventData.eventDataParms.event->where;
  221.                         GlobalToLocal (&where);
  222.                         (void)MCClick (gPreviewMC, callBackParms->window, where, (SInt32)callBackParms->eventData.eventDataParms.event->when, callBackParms->eventData.eventDataParms.event->modifiers);
  223.                         break;
  224.                     case kHighLevelEvent:
  225.                         err = AEProcessAppleEvent (callBackParms->eventData.eventDataParms.event);
  226.                         break;
  227.                 }
  228.             }
  229.             break;
  230.         case kNavCBAdjustRect:
  231.             break;
  232.         case kNavCBAdjustPreview:
  233.             if (gPreviewMC != nil) {
  234.                 GetMovieNaturalBoundsRect (gPreviewMovie, &boundsRect);
  235.                 // Set movie's origin to 0,0.
  236.                 boundsRect.bottom -= boundsRect.top;
  237.                 boundsRect.bottom += 16;    // Room for the controller.
  238.                 boundsRect.right -= boundsRect.left;
  239.                 boundsRect.top = 0;
  240.                 boundsRect.left = 0;
  241.                 DisposeMovieController (gPreviewMC);
  242.                 gPreviewMC = nil;
  243.                 ScaleImageToRect (&boundsRect, &callBackParms->previewRect, &boundsRect);
  244.                 gPreviewMC = NewMovieController (gPreviewMovie, &boundsRect, mcScaleMovieToFit);
  245.  
  246.                 if (gPreviewMC != nil) {
  247.                     (void)MCDraw (gPreviewMC, callBackParms->window);
  248.                 }
  249.             }
  250.             break;
  251.         case kNavCBTerminate:
  252.             if (gPreviewMovie != nil) {
  253.                 StopMovie (gPreviewMovie);
  254.                 DisposeMovieController (gPreviewMC);
  255.                 DisposeMovie (gPreviewMovie);
  256.                 gPreviewMC = nil;
  257.                 gPreviewMovie = nil;
  258.                 gLastFilesID = 0;
  259.             }
  260.             break;
  261.     }
  262. }
  263.  
  264. OSErr NavGetFilePreview (void) {
  265.     OSErr                        err;
  266.     NavReplyRecord                navReply;
  267.     NavTypeListHandle            openTypeList;
  268.     NavDialogOptions            dialogOptions;
  269.  
  270.     err = NavGetDefaultDialogOptions (&dialogOptions);
  271.     if (err == noErr) {
  272.         dialogOptions.dialogOptionFlags = kNavAllFilesInPopup | kNavAllowMultipleFiles | kNavAllowPreviews;
  273.         openTypeList = (NavTypeListHandle)GetResource ('open', 128);
  274.         if (openTypeList == nil) {
  275.             err = ResError ();
  276.             if (err == noErr) {
  277.                 err = memFullErr;
  278.             }
  279.         }
  280.     }
  281.  
  282.     if (err == noErr) {
  283.         NavEventUPP                    eventProc;
  284.         NavPreviewUPP                previewProc;
  285.         NavObjectFilterUPP            filterProc;
  286.  
  287.         eventProc = NewNavEventProc (NavEventProc);
  288.         previewProc = NewNavPreviewProc (NavPreviewProc);
  289.         filterProc = NewNavObjectFilterProc (NavFileFilterProc);
  290.         err = NavGetFile (nil, &navReply, &dialogOptions, eventProc, previewProc, filterProc, nil, nil); //openTypeList
  291.         DisposeNavEventUPP (eventProc);
  292.         DisposeNavPreviewUPP (previewProc);
  293.         DisposeNavObjectFilterUPP (filterProc);
  294.     }
  295.  
  296.     if (navReply.validRecord && err == noErr) {
  297.         ProcessSerialNumber            processSN        = {0, kCurrentProcess};
  298.         AEAddressDesc                targetAddress    = {typeNull, nil};
  299.         AppleEvent                    theODOC            = {typeNull, nil},
  300.                                     theReply        = {typeNull, nil};
  301.  
  302.         // Create an Apple Event to ourselves.
  303.         err = AECreateDesc (typeProcessSerialNumber, &processSN, sizeof (ProcessSerialNumber), &targetAddress);
  304.  
  305.         if (err == noErr) {
  306.             // Create the open document event.
  307.             err = AECreateAppleEvent (kCoreEventClass, kAEOpenDocuments, &targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theODOC);
  308.             AEDisposeDesc (&targetAddress);
  309.         }
  310.  
  311.         if (err == noErr) {
  312.             // Put the list of files into the open document event Apple Event.
  313.             err = AEPutParamDesc (&theODOC, keyDirectObject, &(navReply.selection));
  314.         }
  315.  
  316.         if (err == noErr) {
  317.             // Send the open document event to ourselves.
  318.             err = AESend (&theODOC, &theReply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  319.             AEDisposeDesc (&theODOC);
  320.             AEDisposeDesc (&theReply);
  321.         }
  322.  
  323.     }
  324.  
  325.     (void)NavDisposeReply (&navReply);
  326.  
  327.     if (openTypeList != nil) {
  328.         ReleaseResource ((Handle)openTypeList);
  329.     }
  330.  
  331.     return err;
  332. }
  333.  
  334.